//
//  BookRideInfoViewController.m
//  Yogofly
//
//  Created by Meracom on 6/20/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "BookRideInfoViewController.h"
#import "InfoViewController.h"
#import "UINavigationBar+CustomImage.h"
#import "Utility.h"
#import "AppDelegate.h"
#import "CustomCellBookRide.h"
#import "SBJSON.h"
#import "JSON.h"

@implementation BookRideInfoViewController

@synthesize lblDate, lblDistance, lblDrop, lblPickUp;
@synthesize strDate, strDrop, strPickUp, tbView, strCityName;
@synthesize arrayTypeCar;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Step 2";
    
    [[Utility Shared] loadNavigationIOS5];
    arrayTypeCar = [[NSMutableArray alloc] init];
    
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(showViewInfo)
                                                 name:@"CellNotification"
                                               object:nil];

    UIImage *imgHome = [UIImage imageNamed:@"iconRight.png"];
    UIButton *btnHome = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnHome setBackgroundImage:imgHome forState:UIControlStateNormal];
    [btnHome addTarget:self action:@selector(onHome) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *btnRight = [[UIBarButtonItem alloc] initWithCustomView:btnHome];
    self.navigationItem.rightBarButtonItem= btnRight;
    btnRight.customView.frame = CGRectMake(0.0f, 0.0f, imgHome.size.width-24, imgHome.size.height-15);
    [btnRight release];
    
    UIImage *imgBack = [UIImage imageNamed:@"iconLeft.png"];
    UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnBack setBackgroundImage:imgBack forState:UIControlStateNormal];
    [btnBack addTarget:self action:@selector(onBack) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *btnLeft = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
    self.navigationItem.leftBarButtonItem= btnLeft;
    btnLeft.customView.frame = CGRectMake(0.0f, 0.0f, imgBack.size.width-24, imgBack.size.height-15);
    [btnLeft release];
    // Do any additional setup after loading the view from its nib.
    
    lblDate.text = [NSString stringWithFormat:@"%@",strDate]; 
    lblPickUp.text = [NSString stringWithFormat:@"%@",strPickUp];
    lblDrop.text = [NSString stringWithFormat:@"%@",strDrop];
    
    [self getServiceArea];
}

- (void)getServiceArea {
    NSString *strURLSerVice = [NSString stringWithFormat:@"http://ddesk.ankinfosystem.com/yogo/index.php?c=webservices&m=getFlatRatesByCityName&cityName=%@",strCityName];
    
    NSURL *URLService = [NSURL URLWithString:strURLSerVice];
    NSError *error;
    NSString *strContent = [NSString stringWithContentsOfURL:URLService 
                                                       encoding:NSASCIIStringEncoding
                                                          error:&error];
    
    
    NSDictionary* result;
    // convert to object
    SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
    result = [jsonParser objectWithString:strContent]; 
    
    //arrayTypeCar = [result objectForKey:@"data"];
    //arrayTypeCar = [NSMutableArray arrayWithArray:[result objectForKey:@"data"]];
    arrayTem = [[NSArray alloc] initWithArray:[result objectForKey:@"data"]];
    //NSLog(@"%@",[arrayTypeCar description]);
    
    //NSLog(@"%@",[arrayTypeCar objectAtIndex:0]);
}

- (void)onBack{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)onHome{
    [[AppDelegate navigationControllerInstance] popToRootViewControllerAnimated:YES];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)showViewInfo{
    InfoViewController *vcInfo = [[InfoViewController alloc] initWithNibName:@"InfoViewController" bundle:nil];
    [self.navigationController pushViewController:vcInfo animated:YES];
    [vcInfo release];
}

#pragma mark UITableView

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (arrayTem == nil)
        return 0;
    else
        return [arrayTem count];
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier =@"CustomCellBookRide";
    CustomCellBookRide *cell = (CustomCellBookRide*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {   
        NSArray*    nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCellBookRide" owner:(id)[CustomCellBookRide class] options:nil];
        cell = (CustomCellBookRide *)[nib objectAtIndex:0];
        //cell.cellType = kcellTypeMultipleChoice;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundView.backgroundColor = [UIColor clearColor]; 
    }
    NSMutableDictionary *dicTem = [arrayTem objectAtIndex:indexPath.row];
    
    cell.lblPrice.text = [dicTem valueForKey:@"price"];
    cell.lblType.numberOfLines = 2;
    
    cell.lblType.text = [[arrayTem objectAtIndex:indexPath.row] valueForKey:@"carType"];
    
    return cell;
}


@end
